import { login } from "@/api/login";
import { info } from "@/api/home.js";
import router from "@/router";
import { Message } from "element-ui";
export default {
  namespaced: true,
  state: {
    token: "",
    infoList: {},
  },
  mutations: {
    // 保持token
    SETTOKEN(state, value) {
      state.token = value;
    },
    // 删除token
    REMOVETOKEN(state) {
      state.token = "";
      state.infoList = "";
    },
    // 保持用户信息
    SETINFO(state, value) {
      state.infoList = value;
    },
  },

  actions: {
    // 获得token
    async gettoken(ctx, value) {
      const res = await login(value);
      console.log(res);
      ctx.commit("SETTOKEN", res.data.token);
      Message.success("登录成功");
      router.push("/layout/dataList");
    },
    // 获得用户的信息
    async getinfo(ctx) {
      const res = await info();
      ctx.commit("SETINFO", res.data);
      if (res.data.status === 0) {
        ctx.commit("REMOVETOKEN");
        Message.error("账号被禁止");
        router.push("/login");
      }
      console.log("用户信息", res);
    },
  },
};
